﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockCommon;
using MockBusiness;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation
{
    public partial class Inbox : System.Web.UI.Page
    {
        private MessageBusiness _messageBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private ListView _messageListView;
        private Label _contentLabel;
        private Label _titleLabel;
        private Label _fromLabel;
        private Label _dateLabel;
        private Label _toLabel;
        private Label _fromBottom;
        private Label _sentBottom;
        private Label _toBottom;
        private Label _titleBottom;
        private Button _inboxButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _messageBusiness = MessageBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _messageListView = (ListView)ListMessageUserControl.FindControl("MessageListView");
                _contentLabel = (Label)ViewMailUserControl.FindControl("ContentLabel");
                _titleLabel = (Label)ViewMailUserControl.FindControl("TitleLabel");
                _fromLabel = (Label)ViewMailUserControl.FindControl("FromLabel");
                _dateLabel = (Label)ViewMailUserControl.FindControl("DateLabel");
                _toLabel = (Label)ViewMailUserControl.FindControl("ToLabel");
                _fromBottom = (Label)ViewMailUserControl.FindControl("FromBottom");
                _sentBottom = (Label)ViewMailUserControl.FindControl("SentBottom");
                _toBottom = (Label)ViewMailUserControl.FindControl("ToBottom");
                _titleBottom = (Label)ViewMailUserControl.FindControl("TitleBottom");
                _inboxButton = (Button)ViewMailUserControl.FindControl("InboxButton");
                _inboxButton.Click += new EventHandler(InboxButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                if (Request.QueryString["mid"] != null)
                {
                    InboxMultiview.ActiveViewIndex = 1;
                    string messageId = Request.QueryString["mid"].ToString();
                    LoadMessage(messageId);
                }
                else
                {
                    InboxMultiview.ActiveViewIndex = 0;
                    User user = (User)Session["user"];
                    LoadMessages(user.UserId);
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        private void LoadMessages(Guid userId)
        {
            try
            {
                _messageListView.DataSource = _messageBusiness.GetAllByUserId(userId);
                _messageListView.DataBind();
                _messageBusiness.UpdateReceiveAll(userId);
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        private void LoadMessage(object messageId)
        {
            try
            {
                Message message = _messageBusiness.GetById(messageId);
                if (message != null)
                {
                    _contentLabel.Text = message.Content;
                    _titleLabel.Text = message.Title;
                    _titleBottom.Text = message.Title;
                    _fromLabel.Text = message.From.UserName;
                    _fromBottom.Text = message.From.UserName;
                    _toLabel.Text = message.To.UserName;
                    _toBottom.Text = message.To.UserName;
                    _dateLabel.Text = message.SendDate.ToString("dddd mm/dd/yyyy hh:mm", CultureInfo.CurrentCulture);
                    _sentBottom.Text = message.SendDate.ToString("dddd mm/dd/yyyy hh:mm", CultureInfo.CurrentCulture);
                }
                if (!message.IsRead)
                {
                    message.IsRead = true;
                    _messageBusiness.Update(message);
                }
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void InboxButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("Inbox.aspx");
        }
    }
}